package cn.yuhe.org.service.impl;


import cn.yuhe.basic.service.impl.BaseServiceImpl;
import cn.yuhe.basic.utils.PageList;
import cn.yuhe.org.domain.Department;
import cn.yuhe.org.mapper.DepartmentMapper;
import cn.yuhe.org.query.DepartmentQuery;
import cn.yuhe.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;





    @Transactional
    @Override
    public void save(Department department) {
        departmentMapper.save(department);
        // 给dirPath拼接值
        String dirPath = "";
        if (department.getParent() == null){
            // 如果parent对象为Null,说明他是顶级对象，那么dirPath就是他自己的主键id
            dirPath = "/" + department.getId();
        }else {
            // 否则他就有上级部门，那么我们需要查询到上级部门的对象，获取到上级部门的dirpath，拼接我自己的id，就是我的dirpath
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            if(parentDept != null){
                dirPath = parentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {

        // 给dirPath拼接值
        String dirPath = "";
        if (department.getParent() == null){
            // 如果parent对象为Null,说明他是顶级对象，那么dirPath就是他自己的主键id
            dirPath = "/" + department.getId();
        }else {
            // 否则他就有上级部门，那么我们需要查询到上级部门的对象，获取到上级部门的dirpath，拼接我自己的id，就是我的dirpath
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            if(parentDept != null){
                dirPath = parentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);

    }



    @Override
    public List<Department> tree() {
        // 1.一次性查询出全部数据
        List<Department> depts = departmentMapper.loadAll();

        // 2.创建一个集合对象，用于放最终排列完成的树数据
        List<Department> deptTree = new ArrayList<Department>();

        // 3.将集合数据换转为map，消除时间复杂度问题
        // Map<Long, Department> deptMap = deptTree.stream().collect(Collectors.toMap(Department::getId, item -> item));
        HashMap<Long, Department> deptMap = new HashMap<>();
        for (Department department : depts) {
            deptMap.put(department.getId(), department);
        }

        // 4.从所有数据中找到一级数据
        for (Department deptTmp : depts) {
            // 如果数据没有父级Id，那么就是一级数据
            if(deptTmp.getParentId() == null){
                deptTree.add(deptTmp);
            }else {
                // 从map集合中获取到父对象，将自己添加到父对象的children集合中
                Department department = deptMap.get(deptTmp.getParentId());
                if (department != null){
                    department.getChildren().add(deptTmp);
                }
            }
        }

        return deptTree;
    }

}
